package finalproject.model.studies;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

import finalproject.config.Randomizer;
import finalproject.config.Ressource;
import finalproject.view.StringManager;

/**
 * This class is used to make players pass exams
 * @author Nicolas Bighetti
 *
 */
public class Exam {
	
	/**
	 * The number of question asked in the test
	 */
	private static final int NBR_QUESTION = 3;

	/**
	 * The score one should obtain to succed
	 */
	private static final double MAX_SCORE = 0.75;
	
	/**
	 * The exam the player will pass, contain NBR_QUESTIOn picked from the pool of question
	 */
	private Map<String, Boolean> activeExam;
	
	/**
	 * The exam topic
	 */
	private Topic topic;
	
	/**
	 * The default constructor
	 * @param topic the exam's topic
	 */
	public Exam(Topic topic)
	{
		createExam();
		this.topic = topic;
	}
	
	/**
	 * This method pick random question from the pool availlable in the ressource class an build an exam
	 */
	private void createExam()
	{
		activeExam = new HashMap<String, Boolean>();
		
		
		while(activeExam.size() < NBR_QUESTION)
		{
			activeExam.put("question" + (Randomizer.getRandom().nextInt(Ressource.getNumberOfQuestion())+1), null);
		}
	}
	
	/**
	 * 
	 * @return the next unanwsered question
	 */
	public String getNextQuestion()
	{
		Iterator<String> i = activeExam.keySet().iterator();
		String key;
		
		while(i.hasNext())
		{
			key = i.next();
			if(activeExam.get(key) == null)
			{
				return key;
			}
		}
		return "examOver";
	}
	
	/**
	 * Answer the next unanswered question
	 * @param anwser
	 * @return
	 */
	public boolean answerQuestion(boolean anwser)
	{
		Iterator<String> i = activeExam.keySet().iterator();
		String key;
		
		while(i.hasNext())
		{
			key = i.next();
			if(activeExam.get(key) == null)
			{
				activeExam.put(key, anwser);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Test if the player successfully passsed the exam
	 * @return true if the player is successful, false otherwise
	 */
	public boolean isPassed()
	{
		Iterator<String> i = activeExam.keySet().iterator();
		String key;
		int results = 0;
		
		while(i.hasNext())
		{
			key = i.next();
			if(activeExam.get(key) == null || (activeExam.get(key).equals(Ressource.getAnwser(key))))
			{
				continue;
			}
			
			if(activeExam.get(key).equals(Ressource.getAnwser(key)))
			{
				results++;
			}
		}
		
		if((results%activeExam.size() > MAX_SCORE))
		{
				return true;
		}
		else
		{
			return false;
		}
	}

	public void doExam()
	{
		Scanner sc = new Scanner(System.in);
		StringManager.outPut("examstart");
		String question = getNextQuestion();
		
		while(!(question.equals("examover")))
		{
			try
			{
				StringManager.outPut(question);
				answerQuestion(sc.nextBoolean());
				question = getNextQuestion();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		StringManager.outPut(question);
		
		sc.close();
	}
	
	/**
	 * @return the exam's topic
	 */
	public Topic getTopic() {
		// TODO Auto-generated method stub
		return topic;
	}
	
}
